﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SIMS.BL;
using System.Data;
using MandalaySoft.Common;

public partial class School_GradeLevel : System.Web.UI.Page
{
    MyError _error = new MyError();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[Global.Session_SchoolID] == null)
        {
            Response.Redirect("~/Default.aspx");
        }

        if (!IsPostBack)
        {
            BindData();
        }
    }

    private void BindData()
    {
        GradeLevelDB _db = new GradeLevelDB();
        DataTable _dt = new DataTable("tblGradeLevel");
        _error = new MyError();
        _dt = _db.GetGradeLevelList(Convert.ToInt32(Session[Global.Session_SchoolID].ToString()), out _error);

        gvGradeLevel.DataSource = _dt;
        gvGradeLevel.DataBind();
    }

    protected void OnPaging(object sender, GridViewPageEventArgs e)
    {
        BindData();
        gvGradeLevel.PageIndex = e.NewPageIndex;
        gvGradeLevel.DataBind();
    }

    protected void AddGradeLevel(object sender, EventArgs e)
    {
        string GradeLevelID = ((TextBox)gvGradeLevel.FooterRow.FindControl("txtGradeLevelID")).Text;
        string GradeLevelTitle = ((TextBox)gvGradeLevel.FooterRow.FindControl("txtTitle")).Text;
        string ShortName = ((TextBox)gvGradeLevel.FooterRow.FindControl("txtShortName")).Text;
        string SortOrder = ((TextBox)gvGradeLevel.FooterRow.FindControl("txtSortOrder")).Text;
        string NextGradeLevelID = ((DropDownList)gvGradeLevel.FooterRow.FindControl("ddlNextGradeLevel")).Text;
        bool IsActive = ((CheckBox)gvGradeLevel.FooterRow.FindControl("chkActive")).Checked;

        if (SaveGradeLevel(GradeLevelID, GradeLevelTitle, ShortName, SortOrder, NextGradeLevelID, IsActive) > 0)
        {
            BindData();
        }
    }

    private int SaveGradeLevel(string GradeLevelID, string GradeLevelTitle, string ShortName, string SortOrder, string NextGradeLevelID, bool IsActive)
    {
        GradeLevel _item = new GradeLevel();
        _item.GradeLevelID = Convert.ToInt32(GradeLevelID);
        _item.SchoolID = Convert.ToInt32(Session[Global.Session_SchoolID].ToString());
        _item.Title = GradeLevelTitle;
        _item.ShortName = ShortName;
        _item.NextGradeLevelID = Convert.ToInt32(NextGradeLevelID);
        _item.SortOrder = Convert.ToInt16(SortOrder);
        _item.IsActive = IsActive;

        GradeLevelDB _db = new GradeLevelDB();
        _item = _db.Save(_item);

        return _item.GradeLevelID;
    }

    protected void UpdateGradeLevel(object sender, GridViewUpdateEventArgs e)
    {
        string GradeLevelID = ((Label)gvGradeLevel.Rows[e.RowIndex].FindControl("lblGradeLevelID")).Text;
        string GradeLevelTitle = ((TextBox)gvGradeLevel.Rows[e.RowIndex].FindControl("txtTitle")).Text;
        string ShortName = ((TextBox)gvGradeLevel.Rows[e.RowIndex].FindControl("txtShortName")).Text;
        string SortOrder = ((TextBox)gvGradeLevel.Rows[e.RowIndex].FindControl("txtSortOrder")).Text;
        DropDownList NextGradeLevelID = ((DropDownList)gvGradeLevel.Rows[e.RowIndex].FindControl("ddlNextGradeLevel"));
        bool IsActive = ((CheckBox)gvGradeLevel.Rows[e.RowIndex].FindControl("chkActive")).Checked;

        if (SaveGradeLevel(GradeLevelID, GradeLevelTitle, ShortName, SortOrder, NextGradeLevelID.Text, IsActive) > 0)
        {
            gvGradeLevel.EditIndex = -1;
            BindData();
        }
    }

    protected void DeleteGradeLevel(object sender, EventArgs e)
    {
        LinkButton lnkRemove = (LinkButton)sender;
        GradeLevelDB _db = new GradeLevelDB();
        _error = new MyError();
        _db.Delete(Convert.ToInt32(lnkRemove.CommandArgument), out _error);
        if (_error.ErrorNo == 0)
        {
            BindData();
        }
    }

    protected void EditGradeLevel(object sender, GridViewEditEventArgs e)
    {
        gvGradeLevel.EditIndex = e.NewEditIndex;
        CheckBox chkTakeAttendence = (CheckBox)gvGradeLevel.Rows[e.NewEditIndex].FindControl("chkTakeAttendence");
        Label lblTakeAttendence = (Label)gvGradeLevel.Rows[e.NewEditIndex].FindControl("lblTakeAttendence");

        if (chkTakeAttendence != null && lblTakeAttendence != null)
        {
            if (lblTakeAttendence.Text.ToUpper() == "YES")
            {
                chkTakeAttendence.Checked = true;
            }
            else
            {
                chkTakeAttendence.Checked = false;
            }
        }
        BindData();
    }

    protected void CancelEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gvGradeLevel.EditIndex = -1;
        BindData();
    }

    protected void gvGradeLevel_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DataRowView drv = (DataRowView)e.Row.DataItem;
            if (drv != null)
            {
                Label lblActive = (Label)e.Row.FindControl("lblActive");
                Label lblNextGradeLevel = (Label)e.Row.FindControl("lblNextGradeLevel");
                CheckBox chkActive = (CheckBox)e.Row.FindControl("chkActive");
                DropDownList ddlNextGradeLevel = (DropDownList)e.Row.FindControl("ddlNextGradeLevel");
                if (lblActive != null)
                {
                    if (drv["IsActive"] != null)
                    {
                        if (Convert.ToBoolean(drv["IsActive"].ToString()) == true)
                        {
                            lblActive.Text = "Yes";
                        }
                        else
                        {
                            lblActive.Text = "No";
                        }
                    }
                }

                if (lblNextGradeLevel != null)
                {
                    if (drv["NextGradeLevelTitle"] != null)
                    {
                        lblNextGradeLevel.Text = drv["NextGradeLevelTitle"].ToString();
                    }
                }

                if (chkActive != null)
                {
                    if (drv["IsActive"] != null)
                    {
                        if (Convert.ToBoolean(drv["IsActive"].ToString()) == true)
                        {
                            chkActive.Checked = true;
                        }
                        else
                        {
                            chkActive.Checked = false;
                        }
                    }
                }

                if (ddlNextGradeLevel != null)
                {
                    _error = new MyError();
                    General.BindGradeLevelList(ddlNextGradeLevel, Convert.ToInt32(Session[Global.Session_SchoolID].ToString()), string.Empty, true, out _error);
                    ddlNextGradeLevel.Items.Insert(0, new ListItem("Select", "-1"));

                    if (drv["NextGradeLevelID"] != null)
                    {
                        ddlNextGradeLevel.Text = drv["NextGradeLevelID"].ToString();
                    }
                }
            }
        }
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            DropDownList ddlNextGradeLevel = (DropDownList)e.Row.FindControl("ddlNextGradeLevel");
            _error = new MyError();
            General.BindGradeLevelList(ddlNextGradeLevel, Convert.ToInt32(Session[Global.Session_SchoolID].ToString()), string.Empty, true, out _error);
            ddlNextGradeLevel.Items.Insert(0, new ListItem("Select", "-1"));
        }
    }

    protected void gvGradeLevel_RowCreated(object sender, GridViewRowEventArgs e)
    {
        //if ((e.Row.RowState == DataControlRowState.Edit) || (e.Row.RowState == (DataControlRowState.Edit | DataControlRowState.Alternate)))
        //{
        //    DataRowView drv = (DataRowView)e.Row.DataItem;

        //    if (drv != null)
        //    {
        //        CheckBox chkActive = (CheckBox)e.Row.FindControl("chkActive");
        //        DropDownList ddlNextGradeLevel = (DropDownList)e.Row.FindControl("ddlNextGradeLevel");

        //        if (chkActive != null)
        //        {
        //            if (drv["IsActive"] != null)
        //            {
        //                if (Convert.ToBoolean(drv["IsActive"].ToString()) == true)
        //                {
        //                    chkActive.Checked = true;
        //                }
        //                else
        //                {
        //                    chkActive.Checked = false;
        //                }
        //            }
        //        }   

        //        if (ddlNextGradeLevel != null)
        //        {
        //            _error = new MyError();
        //            General.BindGradeLevelList(ddlNextGradeLevel, Convert.ToInt32(Session[Global.Session_SchoolID].ToString()), string.Empty, true, out _error);
        //            ddlNextGradeLevel.Items.Insert(0, new ListItem("Select", "-1"));

        //            if (drv["NextGradeLevelID"] != null)
        //            {
        //                if (drv["NextGradeLevelID"].ToString() != string.Empty)
        //                {
        //                    ddlNextGradeLevel.Text = drv["NextGradeLevelID"].ToString();
        //                }
        //            }
        //        }
        //    }
        //}
    }

    public static void BindGradeLevelList(DropDownList ddlGradeLevel, int SchoolID, string filter, bool IsFullName, out MyError error)
    {
        DataTable _dt = new DataTable();
        GradeLevelDB _db = new GradeLevelDB();
        error = new MyError();
        _dt = _db.GetGradeLevelList(filter, SchoolID, IsFullName, out error);
        ddlGradeLevel.Items.Clear();
        if (error.ErrorNo == 0)
        {
            if (_dt != null && _dt.Rows.Count > 0)
            {
                foreach (DataRow _dr in _dt.Rows)
                {
                    ddlGradeLevel.Items.Add(new ListItem(_dr["GradeLevelName"].ToString(), _dr["GradeLevelID"].ToString()));
                }
            }
        }
    }
}